package fr.ants.model;

/**
 * Resource unit.
 * 
 * @author Vianney DUPOY DE GUITARD.
 */
public class Resource {

	/** The quantity of food the resource can have. */
	private float capacity;

	/** The quantity of food the resource has. */
	private float amount;

	/** Regeneration rate of this resource. */
	private float regenRate;

	/**
	 * Creates a new resource.
	 * 
	 * @param pCapacity
	 *            : capacity of the resource.
	 * @param pRegenRate
	 *            : regeneration rate of the resource.
	 */
	public Resource(int pCapacity, float pRegenRate) {
		capacity = pCapacity;
		amount = pCapacity;
		regenRate = pRegenRate;
	}

	/**
	 * Creates a new resource.
	 * 
	 * @param pCapacity
	 *            : capacity of the resource.
	 * @param pAmount
	 *            : amount of resource available.
	 * @param pRegenRate
	 *            : regeneration rate of the resource.
	 */
	public Resource(int pCapacity, int pAmout, float pRegenRate) {
		capacity = pCapacity;
		amount = pAmout;
		regenRate = pRegenRate;
	}

	/**
	 * Consume the resource by a quantity.
	 * 
	 * @param qty
	 *            : the quantity of food to take.
	 * @return the amount of food that could be took away.
	 */
	public float consume(float qty) {
		float consumption = 0;
		if (qty <= amount) {
			consumption = qty;
			amount -= qty;
		} else {
			consumption = amount;
			amount = 0;
		}
		return consumption;
	}

	/** Regenerate the resource with its regeneration rate. */
	public void regen() {
		if (amount < capacity) {
			amount += regenRate;
		}
	}

	/** @return the current amount of food present. */
	public float getAmount() {
		return amount;
	}

	/** @return the amount of food this resource can hold. */
	public float getCapacity() {
		return capacity;
	}

	/**
	 * set the capacity.
	 * 
	 * @param value
	 */
	public void setCapacity(float capacity) {
		this.capacity = capacity;
	}

	/** @return true if the resource is empty. */
	public boolean isEmpty() {
		return amount == 0;
	}

	/** Resets the resource. */
	public void reset() {
		amount = capacity;
	}

	/**
	 * @return the regen rate.
	 */
	public float getRegenRate() {
		return regenRate;
	}

	/**
	 * set the regen rate value
	 * 
	 * @param value
	 */
	public void setRegenRate(float regenRate) {
		this.regenRate = regenRate;
	}
}